package penguin.masking;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wuxh
 * @version 1.0.0
 */
public class DefaultCache implements DataReductionCache {

    private final ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, String> valueCache = new ConcurrentHashMap<>();

    @Override
    public String addNode(String key, String value) {
        cache.put(key, value);
        String _value = StringUtil.mdfIfLong(value);
        valueCache.put(_value, key);
        return key;
    }

    @Override
    public String value(String key) {
        return cache.get(key);
    }

    @Override
    public String key(String value) {
        String _value = StringUtil.mdfIfLong(value);
        String key = valueCache.get(_value);
        return key;
    }

    @Override
    public long initId() {
        return 1;
    }

    @Override
    public void free() {
        cache.clear();
        valueCache.clear();
    }
}
